﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace OrderControl {
    class Program {
        static void Show(List<Order> orders) {
            foreach (Order order in orders) {
                Console.WriteLine(order);
                Console.WriteLine(">>" + order.Customer);
                foreach (OrderDetail detail in order.Details) {
                    Console.WriteLine(">>" + detail);
                }
            }
            Console.WriteLine();
        }
        static void Main(string[] args) {
            try {
                Commodity rice = new Commodity("Rice", 12.5);
                Commodity noodle = new Commodity("Noodle", 15);
                Commodity vermicelli = new Commodity("Vermicelli", 16);

                Customer jack = new Customer("Jack", "12 years old");
                Customer mike = new Customer("Mike");
                Customer mary = new Customer("Mary", "like noodle");

                Order jackOrder = new Order(1, jack, new DateTime(2021, 3, 30), "Wuhan");
                Order mikeOrder = new Order(3, mike, new DateTime(2021, 3, 31), "BeiJing");

                OrderService service = OrderService.Instance;

                service.Add(jackOrder);
                service.Add(mikeOrder);

                service.DetailAdd(jackOrder, rice, 5);
                service.DetailAdd(jackOrder, noodle, 3);
                service.DetailAdd(mikeOrder, vermicelli, 5);

                Console.WriteLine("<Original Order>");
                Show(service.Orders);

                OrderService.Export();

                Console.WriteLine("<Modify Order>");
                service.DetailModify(jackOrder, rice, 3);
                service.DetailDelete(jackOrder, noodle);
                Show(service.Orders);

                Console.WriteLine("<Add Order>");
                service.Add(new Order(7, mary, new DateTime(2020, 5, 8), "ShangHai"));
                Show(service.Orders);

                Console.WriteLine("<Search Order>");
                Console.WriteLine("Order (Id = 7):");
                var result = service.Search(OrderService.SearchType.Id, 7);
                Show(result.ToList());

                Console.WriteLine("<Sort Order>");
                Console.WriteLine("Sort by Total Price:");
                service.Sort((orderA, orderB) => {
                    if (orderA.TotalPrice > orderB.TotalPrice) {
                        return 1;
                    }
                    else if (orderA.TotalPrice == orderB.TotalPrice) {
                        return 0;
                    }
                    else {
                        return -1;
                    }
                });
                Show(service.Orders);
                Console.WriteLine("Sort by Id:");
                service.Sort();
                Show(service.Orders);

                Console.WriteLine("<Delete Order>");
                service.Delete(7);
                Show(service.Orders);

                Console.WriteLine("<Import Order>");
                OrderService.Import();
                Show(OrderService.Instance.Orders);
            }
            catch (OrderPropertyException e) {
                Console.WriteLine(e);
            }
            catch (OrderSearchException e) {
                Console.WriteLine(e);
            }
            catch (OrderAddException e) {
                Console.WriteLine(e);
            }
            catch (OrderDeleteException e) {
                Console.WriteLine(e);
            }
            catch (OrderModifyException e) {
                Console.WriteLine(e);
            }
            catch (Exception e) {
                Console.WriteLine(e);
            }
        }
    }
}
